﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using YiSha.Data.Repository;
using YiSha.Entity.OrganizationManage;
using YiSha.Enums;
using YiSha.Model.Param.OrganizationManage;
using YiSha.Model.Result.News;
using YiSha.Service.SystemManage;
using YiSha.Util;
using YiSha.Util.Model;

namespace YiSha.Service.OrganizationManage
{
    /// <summary>
    /// 文章服务
    /// </summary>
    public class NewsService : RepositoryFactory
    {
        public AreaService AreaService { get; set; }
        public ImaService ImaService { get; set; }
        #region 获取数据
        public async Task<TData<List<NewsResult>>> GetList(NewsListParam param)
        {
            TData<List<NewsResult>> obj = new TData<List<NewsResult>>();
            var expression = ExtLinq.True<NewsResult>();
            if (!param.ProvinceId.IsEmpty())
            {
                expression = expression.And(a => a.ProvinceId == param.ProvinceId);
            }
            if (!param.CityId.IsEmpty())
            {
                expression = expression.And(a => a.CityId == param.CityId);
            }
            if (!param.CountyId.IsEmpty())
            {
                expression = expression.And(a => a.CountyId == param.CountyId);
            }
            if (!param.NewsTitle.IsEmpty())
            {
                expression = expression.And(a => a.NewsTitle == param.NewsTitle);
            }
            if (!param.NewsTag.IsEmpty())
            {
                expression = expression.And(a => a.NewsTag == param.NewsTag);
            }
            obj.Data = SqlSugarAdapters.GetList(expression);
            return obj;
        }

        public async Task<TData<List<NewsResult>>> GetPageList(NewsListParam param, Pagination pagination)
        {
            TData<List<NewsResult>> obj = new TData<List<NewsResult>>();
            var expression = ExtLinq.True<NewsResult>();
            if (!param.ProvinceId.IsEmpty())
            {
                expression = expression.And(a => a.ProvinceId == param.ProvinceId);
            }
            if (!param.CityId.IsEmpty())
            {
                expression = expression.And(a => a.CityId == param.CityId);
            }
            if (!param.CountyId.IsEmpty())
            {
                expression = expression.And(a => a.CountyId == param.CountyId);
            }
            if (!param.NewsTitle.IsEmpty())
            {
                expression = expression.And(a => a.NewsTitle == param.NewsTitle);
            }
            if (!param.NewsTag.IsEmpty())
            {
                expression = expression.And(a => a.NewsTag == param.NewsTag);
            }
            var list = SqlSugarAdapters.GetPager(expression, pagination);
            var info = list.Item2.ToList();
            //获取图片信息
            var imgId = info.Select(s => s.ThumbImage).ToList();
            var imgInfo = ImaService.GetList(imgId);
            list.Item2.ToList().ForEach(f =>
             {
                 var temp = imgInfo.Where(w => w.Id==f.ThumbImage).FirstOrDefault();
                 if (!temp.IsEmpty())
                 {
                     f.ImgUrl = temp.ImagePath;
                 }
             });
            obj.Data = list.Item2;
            obj.Total = list.Item1;
            return obj;
        }

        public async Task<TData<List<NewsEntity>>> GetPageContentList(NewsListParam param, Pagination pagination)
        {
            TData<List<NewsEntity>> obj = new TData<List<NewsEntity>>();
            var expression = ExtLinq.True<NewsEntity>();
            if (!param.ProvinceId.IsEmpty())
            {
                expression = expression.And(a => a.ProvinceId == param.ProvinceId);
            }
            if (!param.CityId.IsEmpty())
            {
                expression = expression.And(a => a.CityId == param.CityId);
            }
            if (!param.CountyId.IsEmpty())
            {
                expression = expression.And(a => a.CountyId == param.CountyId);
            }
            if (!param.NewsTitle.IsEmpty())
            {
                expression = expression.And(a => a.NewsTitle == param.NewsTitle);
            }
            if (!param.NewsTag.IsEmpty())
            {
                expression = expression.And(a => a.NewsTag == param.NewsTag);
            }
            var list = SqlSugarAdapters.GetPager(expression, pagination);
            obj.Data = list.Item2.ToList();
            obj.Total = list.Item1;
            return obj;
        }

        public async Task<TData<NewsEntity>> GetEntity(string id)
        {
            TData<NewsEntity> obj = new TData<NewsEntity>();
            obj.Data = SqlSugarAdapters.GetById<NewsEntity>(id);
            return obj;
        }


        #endregion

        #region 提交数据
        /// <summary>
        /// 提交数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<TData<string>> SaveForm(NewsEntity entity)
        {
            TData<string> obj = new TData<string>();
            try
            {
                if (ExistName(entity))
                {
                    obj.MsgCode = MsgCode.Fail;
                    obj.Msg = "当前名称已经存在！";
                    return obj;
                }
                if (entity.Id.IsEmpty())
                {
                    _ = entity.Create();
                    this.SqlSugarAdapters.Insert(entity);
                }
                else
                {
                    _ = entity.Modify();
                    this.SqlSugarAdapters.Update(entity);
                }
                return obj;
            }
            catch (Exception ex)
            {
                obj.MsgCode = MsgCode.Fail;
                obj.Msg = ex.Message;
                return obj;
            }
        }
        /// <summary>
        /// 校验是否存在数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        private bool ExistName(NewsEntity entity)
        {
            var expression = ExtLinq.True<NewsEntity>();
            expression = expression.And(t => t.Valid);
            if (entity.Id.IsEmpty())
            {
                expression = expression.And(t => t.NewsTitle == entity.NewsTitle);
            }
            else
            {
                expression = expression.And(t => t.NewsTitle == entity.NewsTitle && t.Id != entity.Id);
            }
            return SqlSugarAdapters.GetList(expression).Count() > 0 ? true : false;
        }
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<TData> DeleteForm(List<string> ids)
        {
            TData result = new TData();
            try
            {
                var info = SqlSugarAdapters.GetList<NewsEntity>(s => ids.Contains(s.Id));
                if (info.Count > 0)
                {
                    SqlSugarAdapters.Delete<NewsEntity>(s => ids.Contains(s.Id));
                }
                else
                {
                    result.MsgCode = MsgCode.Fail;
                    result.Msg = "当前数据不存在";
                }
            }
            catch (Exception ex)
            {
                result.MsgCode = MsgCode.Fail;
                result.Msg = ex.Message;
            }
            return result;
        }
        #endregion


    }
}
